package com.bootdo.market.controller;

import com.alibaba.fastjson.JSON;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.market.dao.CommonDao;
import com.bootdo.market.dao.DuplicateDao;
import com.bootdo.market.domain.AppDO;
import com.bootdo.market.domain.PlatformAdvunionAccountDO;
import com.bootdo.market.service.PlatformAdvunionAccountService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 平台广告联盟账号
 *
 * @author jinse
 * @email market@jinse.com
 * @date 2019-04-26 15:59:17
 */

@Controller
@RequestMapping("/market/platformAdvunionAccount")
public class PlatformAdvunionAccountController {
    @Autowired
    private PlatformAdvunionAccountService platformAdvunionAccountService;
    @Autowired
    private CommonDao commonDao;
    @Autowired
    private DuplicateDao duplicateDao;

    @GetMapping()
    @RequiresPermissions("market:platformAdvunionAccount:platformAdvunionAccount")
    String PlatformAdvunionAccount() {
        return "market/platformAdvunionAccount/platformAdvunionAccount";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("market:platformAdvunionAccount:platformAdvunionAccount")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<PlatformAdvunionAccountDO> platformAdvunionAccountList = platformAdvunionAccountService.list(query);
        int total = platformAdvunionAccountService.count(query);
        PageUtils pageUtils = new PageUtils(platformAdvunionAccountList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("market:platformAdvunionAccount:add")
    String add(Model model) {
        List<Map<String, Object>> thirdSdklist = commonDao.listAllThirdSdk();
        model.addAttribute("thirdSdklist", thirdSdklist);
        return "market/platformAdvunionAccount/add";
    }


    @RequestMapping(value = "/listAllAdvUnion", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String listAllAdvUnion(HttpServletRequest request) {
        List<Map<String, Object>> userProductList = commonDao.listAllAdvUnion();
        return JSON.toJSONString(userProductList);
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("market:platformAdvunionAccount:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        PlatformAdvunionAccountDO platformAdvunionAccount = platformAdvunionAccountService.get(id);
        model.addAttribute("platformAdvunionAccount", platformAdvunionAccount);
        return "market/platformAdvunionAccount/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("market:platformAdvunionAccount:add")
    public R save(PlatformAdvunionAccountDO platformAdvunionAccount) {
        if (platformAdvunionAccountService.save(platformAdvunionAccount) > 0) {
            List<AppDO> taskspiderrelatList = platformAdvunionAccount.getTaskspiderrelatList();
            if (taskspiderrelatList != null && taskspiderrelatList.size() > 0) {
                taskspiderrelatList.stream().forEach(appDO -> {
                    if (appDO.getThirdSdkId() != null && !"".equals(appDO.getThirdSdkId())) {
                        appDO.setAdvUnionId(platformAdvunionAccount.getAdvunionid());
                        appDO.setUnionAccountId(platformAdvunionAccount.getId());
                        platformAdvunionAccountService.saveAppDO(appDO);
                    }
                });

            }
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("market:platformAdvunionAccount:edit")
    public R update(PlatformAdvunionAccountDO platformAdvunionAccount) {
        platformAdvunionAccountService.update(platformAdvunionAccount);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("market:platformAdvunionAccount:remove")
    public R remove(PlatformAdvunionAccountDO platformAdvunionAccount) {
        platformAdvunionAccountService.update(platformAdvunionAccount);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("market:platformAdvunionAccount:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        platformAdvunionAccountService.batchRemove(ids);
        return R.ok();
    }

    @RequestMapping(value = "/platformAdvunionAccountJudgingUniqueAdd", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public R platformAdvunionAccountJudgingUniqueAdd(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        Map<String, Object> aa = duplicateDao.platformAdvunionAccountJudgingUniqueAdd(params);
        if (aa == null) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping(value = "/platformAdvunionAccountJudgingUniqueEdit", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public R platformAdvunionAccountJudgingUniqueEdit(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        Map<String, Object> aa = duplicateDao.platformAdvunionAccountJudgingUniqueEdit(params);
        if (aa == null) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}
